using System;
using System.Linq;
using System.Collections.Generic;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using SinBiota.Model.Persistence.Repositories.Interfaces;

namespace SinBiota.Model.Business.PersistenceServices.Implementation
{
    public class ProjectPersistenceServices : IProjectPersistenceServices
    {
        private readonly IProjectRepository _projectRepository;
        private readonly IKeywordRepository _keywordRepository;

        public ProjectPersistenceServices(IProjectRepository projectRepository, IKeywordRepository keywordRepository)
        {
            _projectRepository = projectRepository;
            _keywordRepository = keywordRepository;
        }

        public Project FindById(int id)
        {
            return _projectRepository.FindById(id);
        }

        public Project FindByName(string name)
        {
            return _projectRepository.FindByName(name);
        }

        public IList<Project> FindAll()
        {
            return _projectRepository.FindAll();
        }

        public void DeleteById(int id)
        {
            Delete(FindById(id));
        }
        public void Delete(Project project)
        {
            _projectRepository.Delete(project);
        }

        public void Save(Project project, string[] keywordList)
        {
            //Add keywords; create new if does not exist in repository;
            foreach (var name in keywordList)
            {
                project.Keywords.Add(_keywordRepository.FindByEqRestrictionUniqueResult(x => x.Name, name) ?? new Keyword { Name = name });
            }
            _projectRepository.Save(project);
        }

        public void Update(Project project, string[] keywordList)
        {

            //keywords: remove keyword from project if it is not on the 'wordList' from the form:
            IList<Keyword> removeList = project.Keywords.Where(keyword => !keywordList.Contains(keyword.Name)).ToList();
            foreach (var keyword in removeList) project.Keywords.Remove(keyword);

            //now add each keywords from list if it isn't already 
            foreach (var name in keywordList)
            {
                var keyword = _keywordRepository.FindByEqRestrictionUniqueResult(x => x.Name, name);
                // if it is null, it is a new keyword, create it and add to project;
                if (keyword == null)
                {
                    project.Keywords.Add(new Keyword { Name = name });
                }
                // if it exists, add it if the project doesn't have it already 
                else
                {
                    if (!project.Keywords.Contains(keyword)) project.Keywords.Add(keyword);
                }
            }
            // Save
            _projectRepository.SaveOrUpdate(project);
        }

        public void Update(Project project)
        {
            _projectRepository.SaveOrUpdate(project);
        }

        public IList<Project> FindByNameLike(string name)
        {
            return _projectRepository.FindByNameLike(name);
        }

        public IList<Project> FindAllActive()
        {
            return _projectRepository.FindAllActive();
        }
    }
}